package com.xcc.dataStructures.demo08_hash;


import java.util.Scanner;

/**
 * 自定义HashTable
 *
 * @author xiaocheng
 * @date 2020/12/10 9:44
 */
public class MyHashDemo {

    public static void main(String[] args) {
        System.out.println("========HashTab========");

        HashTab hashTab = new HashTab(7);
        while (true) {
            System.out.println();
            System.out.println("s[show]:遍历链表");
            System.out.println("a[add]:添加元素");
            System.out.println("g[get]:获取元素");
            System.out.println("d[del]:删除元素");
            System.out.println("e[exit]:退出");
            System.out.print("请输入方法:");
            Scanner scanner = new Scanner(System.in);
            String key = scanner.next();
            switch (key) {
                case "a":
                    System.out.print("请输入添加的值id:");
                    int id = scanner.nextInt();
                    System.out.print("请输入添加的值name:");
                    String name = scanner.next();
                    hashTab.add(new Node(id, name));
                    break;
                case "g":
                    System.out.print("请输入查找的id:");
                    id = scanner.nextInt();
                    hashTab.get(id);
                    break;
                case "s":
                    hashTab.list();
                    break;
                case "d":
                    System.out.print("请输入查找的id:");
                    id = scanner.nextInt();
                    hashTab.delete(id);
                    break;
                case "e":
                    return;
            }
        }
    }

}

/**
 * 自定义hashTab
 */
class HashTab {
    private NodeList[] nodeLists;
    private int size;

    /**
     * 初始化
     */
    public HashTab(int size) {
        this.size = size;
        nodeLists = new NodeList[size];
        //同时初始化每个链表
        for (int i = 0; i < size; i++) {
            nodeLists[i] = new NodeList();
        }
    }

    /**
     * 添加节点
     */
    public void add(Node node) {
        int no = hash(node.id);
        nodeLists[no].add(node);
    }

    /**
     * 遍历hashtable
     */
    public void list() {
        for (int i = 0; i < size; i++) {
            nodeLists[i].list(i);
        }
    }

    /**
     * 获取对应的hash值
     */
    public int hash(int id) {
        return id % size;
    }

    /**
     * 获取
     */
    public void get(int id) {
        int no = hash(id);
        Node node = nodeLists[no].get(id);
        System.out.println("第 " + (no + 1) + " 条链表查找到为:\t" + node);
    }

    /**
     * 删除
     */
    public void delete(int id) {
        int no = hash(id);
        nodeLists[no].delete(no, id);
    }
}

class NodeList {
    private Node head = null;

    /**
     * 添加node
     */
    public void add(Node node) {
        if (head == null) {
            head = node;
            return;
        }
        //在链表最后添加
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    /**
     * 遍历
     */
    public void list(int no) {
        if (head == null) {
            System.out.println("第 " + (no + 1) + " 条链表为空~~");
            return;
        }
        System.out.print("第 " + (no + 1) + " 条链表为:");
        Node cur = head;
        while (cur != null) {
            System.out.print("\t" + cur);
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 根据id获取指定的node
     */
    public Node get(int id) {
        if (head == null) {
            return null;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.id == id) {
                break;
            }
            cur = cur.next;
        }
        return cur;
    }

    /**
     * 删除节点
     */
    public void delete(int no, int id) {
        if (head == null) {
            System.out.println("第 " + (no + 1) + " 条链表为空~~");
            return;
        }
        Node cur = head;
        if (cur.id == id) {
            head = head.next;
            System.out.println("第 " + (no + 1) + " 条链表中删除: " + id);
            return;
        }
        while (cur.next != null) {
            if (cur.next.id == id) {
                break;
            }
            cur = cur.next;
        }
        if (cur.next == null) {
            System.out.println("第 " + (no + 1) + " 条链表没找到~~");
            return;
        }
        cur.next = cur.next.next;
        System.out.println("第 " + (no + 1) + " 条链表中删除: " + id);
    }
}

class Node {
    public int id;
    public String name;
    public Node next;

    public Node() {
    }

    public Node(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node[" +
                "id=" + id +
                ", name='" + name + '\'' +
                ']';
    }
}
